﻿// FTP_Client.cpp : 定义应用程序的入口点。
//

#include "framework.h"
#include "FTP_Client.h"

#define MAX_LOADSTRING 100

// 全局变量:
HINSTANCE hInst;                                // 当前实例
TCHAR szTitle[MAX_LOADSTRING];                  // 标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING];            // 主窗口类名

// 此代码模块中包含的函数的前向声明:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: 在此处放置代码。

    // 初始化全局字符串
    LoadStringA(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringA(hInstance, IDC_FTPCLIENT, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // 执行应用程序初始化:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_FTPCLIENT));

    MSG msg;

    // 主消息循环:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}



//注册窗口类
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXA wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_FTPCLIENT));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= NULL;//MAKEINTRESOURCEW(IDC_FTPCLIENT);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExA(&wcex);
}

// 保存实例句柄并创建主窗口
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // 将实例句柄存储在全局变量中


   //得到系统显示器宽和高
   MainWinSize.x = GetSystemMetrics(SM_CXSCREEN);
   MainWinSize.y = GetSystemMetrics(SM_CYSCREEN);

   //记录主窗口矩形区位置
   MainRect.left = 2 * MainWinSize.x / 7;
   MainRect.right = 5 * MainWinSize.x / 7;
   MainRect.top = MainWinSize.y / 5;
   MainRect.bottom = 4 * MainWinSize.y / 5;

   //创建主窗口
   HWND hWnd = MAINHWND = CreateWindowA(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, MainRect.left,
	   MainRect.top, MainRect.right - MainRect.left, MainRect.bottom - MainRect.top, nullptr, nullptr,
	   hInstance, nullptr);


   //创建日志输出窗口
   logHwnd = CreateWindowEx(WS_EX_CLIENTEDGE,_T("LISTBOX"), NULL, WS_CHILD | WS_VISIBLE | WS_VSCROLL, 0, 0,
	   MainClientRect.right - MainClientRect.left-30, (MainClientRect.bottom - MainClientRect.top)*4/5,
	   MAINHWND, NULL, hInst, NULL);

   //输入窗口
   inHWnd = CreateWindow(_T("EDIT"), NULL, WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_BORDER, 10,
	   (MainClientRect.bottom - MainClientRect.top) * 4 / 5,  MainClientRect.right - MainClientRect.left - 30,
	   (MainClientRect.bottom - MainClientRect.top) / 5 - 20,
	   MAINHWND, NULL, hInst, NULL);

   EditOldWndProc = SetWindowLong(inHWnd, GWL_WNDPROC, (LONG)EditWinProc);
   
   SendMessageA(hWnd, WM_CREATED, NULL, NULL);

   hFont = MYFONT;

   //设置字体
   SendMessage(logHwnd, WM_SETFONT, (WPARAM)hFont, 1);
   SendMessage(inHWnd, WM_SETFONT, (WPARAM)hFont, 1);


   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//处理主窗口的消息。

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
		case WM_CREATED:
		{
			ctlportObj = new ControlPort(logHwnd);
		}
		break;

		case WM_PAINT:
		{
			PAINTSTRUCT ps;
			HDC hdc = BeginPaint(hWnd, &ps);
			
			//显示日志
			for (int i = 0; i < (int)run_log.size(); i++) {
				SendMessage(logHwnd, LB_ADDSTRING, NULL, (LPARAM)run_log[i].c_str());
			}

			//输入窗口
			TCHAR inbuff[1024] = { 0 };
			GetWindowText(inHWnd, inbuff, 1024);
			SetWindowText(inHWnd, inbuff);

			EndPaint(hWnd, &ps);
		}
		break;

		case WM_SIZE:
		{
			//得到系统显示器宽和高
			MainWinSize.x = GetSystemMetrics(SM_CXSCREEN);
			MainWinSize.y = GetSystemMetrics(SM_CYSCREEN);

			//记录主窗口矩形区位置
			MainRect.left = 2 * MainWinSize.x / 7;
			MainRect.right = 5 * MainWinSize.x / 7;
			MainRect.top = MainWinSize.y / 5;
			MainRect.bottom = 4 * MainWinSize.y / 5;

			GetWindowRect(MAINHWND, &MainClientRect);

			MoveWindow(logHwnd, 0, 0, MainClientRect.right - MainClientRect.left-15,
				(MainClientRect.bottom - MainClientRect.top)*4/5, TRUE);

			MoveWindow(inHWnd, 1, (MainClientRect.bottom - MainClientRect.top) * 4 / 5, 
				MainClientRect.right - MainClientRect.left - 5,
				(MainClientRect.bottom - MainClientRect.top) / 5 - 40, NULL);

			break;
		}

		case WM_DESTROY:
			//释放字体数据结构
			DeleteObject(hFont);
			PostQuitMessage(0);
			break;

		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}


//捕获回车
LRESULT CALLBACK EditWinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
	switch (message)
	{

		case WM_KEYDOWN:
		{
			switch (wParam) {
				//ENTER
				case VK_RETURN: {
					userOption();
					break;
				}
				default:	break;
			}
			break;
		}

		default:
			return CallWindowProc((WNDPROC)EditOldWndProc, hWnd, message, wParam, lParam);
	}
	return 0;
}

void userOption() {
	int textLength = GetWindowTextLength(inHWnd);
	if (textLength > 0) {
		PTCHAR str = (PTCHAR)malloc(sizeof(TCHAR) * (textLength + 1));
		if (str) {
			GetWindowText(inHWnd, str, textLength + 1);
			string cmd = str;
			//有效输入，进一步处理并加到日志列表
			ctlportObj->setUserCmdStr(cmd);
			SetEvent(ctlportObj->UserCommandEvent);
			//清除输入框内容
			SetWindowText(inHWnd, NULL);

			free(str);
		}
		else {
			MessageBox(MAINHWND, TEXT("edit str malloc failed!"), TEXT("ERROR"), MB_OK);
		}
	}
}